Skip to content

Fix: Prevent flaky tests with random values in parametrize#3

Merged
MKaczkow merged 2 commits intoMKaczkow:masterfrom
denini08:master
Apr 26, 2026
Merged

Fix: Prevent flaky tests with random values in parametrize#3
MKaczkow merged 2 commits intoMKaczkow:masterfrom
denini08:master

Conversation

@denini08
Copy link
Copy Markdown
Contributor

@denini08 denini08 commented Apr 21, 2026

Hey! This PR is part of a research project on Python projects.

I noticed this repository focuses on learning Python concepts, so I wanted to help highlight a common issue in tests.

The test test_very_simple uses random values in parametrize. Without a fixed seed, this can lead to flaky tests (different numbers on each run), which makes debugging harder.

I understand that in this specific case nothing will break, since the test has only a simple assertion. Still, Im proposing this change as a best practice and as a learning opportunity.

This PR adds a fixture to set a random seed, making tests deterministic while preserving the idea of testing with multiple values.

Summary by CodeRabbit

Release Notes

  • Tests
    • Improved test determinism by ensuring consistent random data generation across test runs.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

📝 Walkthrough

Walkthrough

A pytest fixture was added to the test file that seeds Python's random module with a fixed value (42) at session scope with autouse enabled. This ensures that any tests using random.randint() calls generate consistent parameter values across multiple test runs.

Changes

Cohort / File(s) Summary
Pytest Fixture Addition
testing/pytest/test_employee_pytest.py
Added session-scoped set_random_seed() fixture with autouse=True to seed the random module with value 42, making parametrized test data deterministic.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Poem

🐰 With seeds of forty-two, so grand,
Randomness bends to my command,
Each test run yields the very same,
Deterministic—a rabbit's fame! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding a fixture to prevent flaky tests caused by random values in pytest parametrize.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
testing/pytest/test_employee_pytest.py (1)

33-60: ⚠️ Potential issue | 🟠 Major

Seed before parametrization, not in a fixture.

pytest fixture bodies run after collection, but the random.randint() calls on lines 52–60 are evaluated when the decorator arguments are built during module import. This fixture does not make these parameters deterministic.

Proposed fix: use a local seeded RNG for parametrized values
-# fixture to ensure reproducible random values
-# without seed, random.randint() produces different values each run (flaky tests)
-@pytest.fixture(scope="session", autouse=True)
-def set_random_seed():
-    """make tests reproducible by seeding the random number generator."""
-    random.seed(42)
-    yield
+_random_values = random.Random(42)
+_random_test_cases = [
+    (_random_values.randint(0, 100),) * 2
+    for _ in range(9)
+]
@@
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
-        tuple([random.randint(0, 100)] * 2),
+        *_random_test_cases,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@testing/pytest/test_employee_pytest.py` around lines 33 - 60, The
parametrized random values are generated at import time so the set_random_seed
fixture doesn't make them deterministic; replace those random.randint(...) calls
in the pytest.mark.parametrize argument with values produced from a local seeded
RNG (e.g. create seeded_rng = random.Random(42) at module scope just above the
pytest.mark.parametrize and call seeded_rng.randint(0, 100) for each tuple) and
keep the set_random_seed fixture as-is; this ensures deterministic params while
keeping set_random_seed for other tests, and refer to the existing
set_random_seed fixture and the pytest.mark.parametrize block when making the
change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@testing/pytest/test_employee_pytest.py`:
- Around line 33-60: The parametrized random values are generated at import time
so the set_random_seed fixture doesn't make them deterministic; replace those
random.randint(...) calls in the pytest.mark.parametrize argument with values
produced from a local seeded RNG (e.g. create seeded_rng = random.Random(42) at
module scope just above the pytest.mark.parametrize and call
seeded_rng.randint(0, 100) for each tuple) and keep the set_random_seed fixture
as-is; this ensures deterministic params while keeping set_random_seed for other
tests, and refer to the existing set_random_seed fixture and the
pytest.mark.parametrize block when making the change.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: fb7b33e4-850c-483a-a1e0-005c4ed7b347

📥 Commits

Reviewing files that changed from the base of the PR and between f17722e and 183866a.

📒 Files selected for processing (1)
  • testing/pytest/test_employee_pytest.py

@MKaczkow
Copy link
Copy Markdown
Owner

Yep, thanks for the fix.

@MKaczkow MKaczkow merged commit 9a1b192 into MKaczkow:master Apr 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants